package com.pcbly.web.persistence.dict;

import java.util.List;

import org.hibernate.Session;

import com.pcbly.web.bean.dict.DictCategoryCombBean;
import com.pcbly.web.bean.dict.DictCategoryFilterBean;
import com.pcbly.web.bean.dict.DictCategoryValueBean;
import com.pcbly.web.components.utils.random.GUID;
import com.pcbly.web.persistence.HibernateBaseDao;

@SuppressWarnings("unchecked")
public class DictCategoryValueHibernateDao extends
		HibernateBaseDao<DictCategoryValueBean> implements DictCategoryValueDao {

	/**
	 * 
	 */
	private static final long serialVersionUID = 978920698388658960L;

	protected Class<DictCategoryValueBean> getEntityClass() {
		return DictCategoryValueBean.class;
	}

	public DictCategoryValueHibernateDao() {
		super();
	}

	@Override
	public List<DictCategoryValueBean> queryListById(String id)
			throws Exception {
		// TODO Auto-generated method stub
		StringBuilder sb = new StringBuilder();
		// sb.append("select a from DictCategoryValueBean a left join DictCategoryCombBean b ");
		// sb.append(" on a.id=b.dictValueId where b.dictId=?  order by ORDERBY_ ");
		sb.append("select a.ID,a.NAME_,a.DESC_,a.ORDERBY_,a.USERDEFINED from ");
		sb.append(" sys_dict_value a left join sys_dict_comb b on ");
		sb.append(" a.ID=b.DICTVALUEID where b.DICTID=? ");
		sb.append("  order by ORDERBY_ ");
		List<DictCategoryValueBean> list = getSession().createSQLQuery(
				sb.toString()).addEntity(DictCategoryValueBean.class)
				.setString(0, id).list();
		return list;
	}

	@Override
	public void createDictCategoryValue(DictCategoryValueBean bean)
			throws Exception {
		// TODO Auto-generated method stub
		Session session = getSession();
		try {
			session.save(bean);
			String childValueId = bean.getChildValueId();
			DictCategoryCombBean comb = new DictCategoryCombBean();
			comb.setDictId(bean.getDictId());
			comb.setDictValueId(bean.getId());
			comb.setId((new GUID()).toString());
			session.save(comb);
			if (!(childValueId == null || "".equals(childValueId.trim()))) {
				String[] arrayId = childValueId.split("::");
				for (int i = 0; i < arrayId.length - 1; i++) {
					DictCategoryFilterBean filter = new DictCategoryFilterBean();
					GUID guid = new GUID();
					filter.setId(guid.toString());
					filter.setMasterId(bean.getDictId());
					filter.setMasterValueId(bean.getId());
					filter.setChildId(bean.getChildId());
					filter.setChildValueId(arrayId[i]);
					session.save(filter);
				}
			}
		} catch (Exception e) {
			e.printStackTrace();
		}
	}

	@Override
	public void updateDictCategoryValue(DictCategoryValueBean bean)
			throws Exception {
		// TODO Auto-generated method stub
		Session session = getSession();
		try {
			session.update(bean);
			String childValueId = bean.getChildValueId();
			// DictCategoryFilterBean filter1=new DictCategoryFilterBean();
			// filter1.setMasterId(bean.getDictId());
			// filter1.setMasterValueId(bean.getId());
			// System.out.println(bean.getDictId());
			session
					.createQuery(
							"delete from DictCategoryFilterBean c where c.masterId=? and c.masterValueId=? ")
					.setString(0, bean.getDictId()).setString(1, bean.getId())
					.executeUpdate();
			if (!(childValueId == null || "".equals(childValueId.trim()))) {
				String[] arrayId = childValueId.split("::");
				for (int i = 0; i < arrayId.length - 1; i++) {
					DictCategoryFilterBean filter = new DictCategoryFilterBean();
					GUID guid = new GUID();
					filter.setId(guid.toString());
					filter.setMasterId(bean.getDictId());
					filter.setMasterValueId(bean.getId());
					filter.setChildId(bean.getChildId());
					filter.setChildValueId(arrayId[i]);
					session.save(filter);
				}
			}
		} catch (Exception e) {
			e.printStackTrace();
		}
	}

	@Override
	public void removeDictCategoryValueByDictId(String id) throws Exception {
		// TODO Auto-generated method stub
	 	StringBuilder sb = new StringBuilder();
		sb.append("delete a from ");
		sb.append(" sys_dict_comb a ");
		sb.append(" LEFT JOIN ");
		sb.append("sys_dict_value b ON ");
		sb.append(" a.DICTVALUEID=b.ID ");
		sb.append("");
		sb.append(" where ");
		sb.append(" a.DICTID in ('");
		sb.append(id.replaceAll(",", "','"));
		sb.append("') ");
		Session session = getSession();
		session.createSQLQuery(sb.toString()).executeUpdate();
	}

}
